/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.ScreenEngine;
import core.Settings;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class CallsignForm extends BasicForm
{
    private static final int SAVE_COMMAND_ID = 1;
    
    private TextField callsign;
    private TextField login;
    private TextField password;
    private TextField serverIP;
    private TextField extraServerIP;
    
    public CallsignForm()
    {
        super("call_sign");
        
        init();
    }
    
    public void show()
    {
        setContent();
 
        //Убираем среднюю кнопку
        if (Display.getInstance().isTouchScreenDevice())
        {
            Display.getInstance().setThirdSoftButton(false); 
        }
        
        super.show();
    }

    public void hideNotify() 
    {
        //Возвращаем среднюю кнопку
        Display.getInstance().setThirdSoftButton(true); 
    }

    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setCyclicFocus(false);

         //Позывной
        addComponent(new Label("call_sign"));
        callsign = new TextField(); 
        callsign.setInputModeOrder(new String[]{"123", "ABC","abc"});
        callsign.setSelectCommandText("");
        callsign.setConstraint(TextArea.ANY);
        addComponent(callsign);
   
        //Логин
        addComponent(new Label("login"));
        login = new TextField();
        login.setInputModeOrder(new String[]{"123", "ABC","abc"});
        login.setSelectCommandText("");
        login.setConstraint(TextArea.ANY);
        addComponent(login);

        //Пароль
        addComponent(new Label("password"));
        password = new TextField();
        password.setInputModeOrder(new String[]{"123", "ABC","abc"});
        password.setSelectCommandText("");
        password.setConstraint(TextArea.ANY | TextArea.PASSWORD);
        addComponent(password);
        
        //ToDo Kvest
        //Поле временное - удалить
        //IP сервета
        addComponent(new Label("IP:"));
        serverIP = new TextField();
        serverIP.setSelectCommandText("");
        serverIP.setConstraint(TextArea.ANY);
        addComponent(serverIP);
        
        //Сервер (резерв)
        addComponent(new Label("extra_server_ip"));
        extraServerIP = new TextField();
        extraServerIP.setSelectCommandText("");
        extraServerIP.setConstraint(TextArea.ANY);
        addComponent(extraServerIP);
        
        addComponent(new Label(" "));
        
        addCommand(new BackCommand());
        
        Command saveCommand = new Command("save", SAVE_COMMAND_ID) 
        {
            public void actionPerformed(ActionEvent evt)
            {                
                save();
                        
                ScreenEngine.getEngine().pop(true);
                
                evt.consume();
            }
        };
        addCommand(saveCommand);
    }
    
    private void save()
    {
        Settings settings = Settings.getSettings();
        
        settings.setCallsign(callsign.getText()) ;
        settings.setLogin(login.getText());
        settings.setPassword(password.getText());
        settings.setServerIP(serverIP.getText());
        settings.setExtraServerIP(extraServerIP.getText());
        
        settings.save();
    }
    
    private void setContent()
    {
        Settings settings = Settings.getSettings();
        
        callsign.setText(settings.getCallsign()) ;
        login.setText(settings.getLogin());
        password.setText(settings.getPassword());
        serverIP.setText(settings.getServerIP());
        extraServerIP.setText(settings.getExtraServerIP());
    }
}
